package com.citytechinc.cmis.client.service
{
	import com.citytechinc.cmis.client.event.ChildrenEvent;
	import com.citytechinc.cmis.client.model.Document;
	import com.citytechinc.cmis.client.model.Folder;
	import com.citytechinc.cmis.client.model.Repository;
	import com.citytechinc.cmis.client.model.Type;
	
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class ChildrenService extends EventDispatcher
	{
		private namespace app = "http://www.w3.org/2007/app";
		private namespace atom = "http://www.w3.org/2005/Atom";
		private namespace cmis = "http://docs.oasis-open.org/ns/cmis/core/200901";
		
		use namespace app;
        use namespace atom;
        use namespace cmis;
        
		private var auth:String;
		
		private var repository:Repository;
		private var folder:Folder;
		
		private var types:ArrayCollection;
		
		public function ChildrenService(auth:String)
		{
			this.auth = auth;
		}
		
		public function getFolderChildren(folder:Folder):void
		{
			this.folder = folder;
			
			getChildren(folder.childrenURL);
		}
		
		public function getRepositoryChildren(repository:Repository):void
		{
			this.repository = repository;
			
			var service:HTTPService = new HTTPService();
			
			service.url = repository.typesURL;
			service.resultFormat = "e4x";
			service.addEventListener(ResultEvent.RESULT, setTypes);
			service.headers["Authorization"] = "Basic " + auth;
			
			service.send();
		}
		
		private function setTypes(event:ResultEvent):void 
		{
			var types:ArrayCollection = new ArrayCollection();
			
			for each (var t:XML in event.result.entry) 
			{
				var type:Type = new Type();
				
				var folderBaseType:String = t.folderType.baseType;
				var folderTypeId:String = t.folderType.typeId;
				
				var documentBaseType:String = t.documentType.baseType;
				var documentTypeId:String = t.documentType.typeId;
				
				if (folderBaseType.length > 0)
				{
					type.baseType = folderBaseType;
					type.id = folderTypeId;
				}
				else if (documentBaseType.length > 0)
				{
					type.baseType = documentBaseType;
					type.id = documentTypeId;
				}
				
				types.addItem(type);			
			}
			
			this.types = types;
			
			getChildren(repository.childrenURL);
			
		}
		
		public function getChildren(url:String):void
		{
			var service:HTTPService = new HTTPService();

			service.url = url;
			service.resultFormat = "e4x";
			service.addEventListener(ResultEvent.RESULT, setChildren);
			service.headers["Authorization"] = "Basic " + auth;
			
			service.send();
		}
		
		private function setChildren(event:ResultEvent):void 
		{
			var folders:ArrayCollection = new ArrayCollection();
			var documents:ArrayCollection = new ArrayCollection();
			
			for each (var f:XML in event.result.entry) 
			{
				var typeId:String = f.object.properties.propertyId.(@name=='ObjectTypeId').value;
				
				var type:Type = getType(typeId);
				
				if (type != null && type.baseType == "folder") 
					folders.addItem(Folder.getFolder(f));
				else if (type != null && type.baseType == "document")
					documents.addItem(Document.getDocument(f));

			}
			
			if (folder == null)
				this.dispatchEvent(new ChildrenEvent(ChildrenEvent.REPO_CHILDREN, null, folders, documents));
			else
			{
				folder.opened = true;
				folder.children = folders;
				folder.documents = documents;
				
				this.dispatchEvent(new ChildrenEvent(ChildrenEvent.FOLDER_CHILDREN, folder, null, documents));
			}
			
		}
		
		private function getType(id:String):Type
		{
			var type:Type;
			
			for (var i:int = 0; i < types.length; i++)
			{
				var t:Type = Type(types.getItemAt(i));
				
				if (t.id == id)
				{
					type = t;
					break;
				}
				
			}
			
			return type;
		}

	}
	
}